<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    /* .foo {
      width: 300px;
      height: 300px;
      background: red;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-top: -150px;
      margin-left: -150px;
    } */
    /* 使用 calc() 函数，我们仅仅通过 top 与 left 属性便能实现相同的效果 */
    .foo {
      width: 300px;
      height: 300px;
      background: red;
      position: absolute;
      top: calc(50% - 150px);
      left: calc(50% - 150px);
    }
  </style>
</head>

<body>
  <div class="foo"></div>
</body>

</html>